$(document).ready(function() {
    dataSource = new kendo.data.DataSource({
        transport: {
            create: {
                url: appBosch.crud.create,
                dataType: "json"
            },
            read: {
                url: appBosch.crud.read,
                dataType: "json"
            },
            update: {
                url: appBosch.crud.update,
                dataType: "json"
            },
            destroy: {
                url: appBosch.crud.destroy,
                dataType: "json"
            },
            parameterMap: function(options, operation) {
                var params = {};
                switch (operation)
                {
                    case 'create':
                        params = {models: kendo.stringify(options.models)};
                        break;
                    case 'read':
                        break;
                    case 'update':
                        params = {models: kendo.stringify(options.models)};
                        break;
                    case 'destroy':
                        params = {id: options.models[0].id};
                        break;
                }
                return params;
            }
        },
        batch: true,
        schema: {
            model: {
                id: "id",
                fields: {
                    course: {
                        editable: true,
                        nullable: false,
                        validation: {
                            required: true,
                            min: 5
                        }
                    },
                    theme: {
                        editable: true,
                        nullable: false
                    },
                    duration: {
                        type: "number",
                        validation: {
                            required: true,
                            min: 5
                        },
                        editable: true,
                        nullable: false
                    },
                    attendant: {
                        editable: true,
                        nullable: false,
                        validation: {
                            required: true
                        }
                    },
                    test_id: {
                        editable: false,
                        nullable: false,
                        defaultValue: appBosch.testId,
                        validation: {
                            required: true
                        }
                    },
                    type: {
                        editable: true,
                        nullable: false,
                        defaultValue: appBosch.types[0].value,
                        validation: {
                            required: true
                        }
                    }
                }
            }
        }
    });

    $("#grid").kendoGrid({
        toolbar: [
            {
                name: "create",
                text: 'Agregar tema'
            },
            {
                name: "pdf",
                text: "Exportar a PDF"
            },
            {
                name: "excel",
                text: "Exportar a Excel"
            }
        ],
        excel: {
            allPages: true,
            fileName: "Temas.xlsx",
            filterable: true
        },
        pdf: {
            allPages: true,
            fileName: "Temas.pdf"
        },
        scrollable: true,
        resizable: true,
        columnMenu: true,
        filterable: true,
        selectable: true,
        navigatable: true,
        sortable: true,
        dataSource: dataSource,
        height: 520,
        columns: [
            {field: "course", title: "Curso"},
            {field: "theme", title: "Tema"},
            {field: "duration", title: "Duración"},
            {field: "attendant", title: "Instructor"},
            {field: "type", title: "Tipo", values: appBosch.types},
            {command: [
                    {
                        name: "edit",
                        text: {
                            edit: "",
                            update: "",
                            cancel: ""
                        }
                    },
                    {
                        name: "destroy",
                        text: ""
                    }], title: "&nbsp;"}],
        editable: {
            confirmation: "¿Estas seguro que deseas eliminar este tema?",
            mode: "popup"
        }
    });

    $("#origen").kendoDropDownList({
        dataTextField: "text",
        dataValueField: "value",
        dataSource: appBosch.tests,
        value: appBosch.testId,
        change: function() {
            var value = this.value();
            var url = appBosch.urlThemes + '/' + value;
            $(location).attr('href', url);
        }
    });
});